package com.jjyu.controller;

import com.jjyu.entity.Feedback;
import com.jjyu.entity.Result;
import com.jjyu.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/feedback")
public class FeedbackController {

    private final FeedbackService feedbackService;

    @Autowired
    public FeedbackController(FeedbackService feedbackService) {
        this.feedbackService = feedbackService;
    }

    @PostMapping("/create")
    public Result<Feedback> createFeedback(@RequestBody Feedback feedback) {
        boolean created = feedbackService.save(feedback);
        if (created) {
            return Result.success(feedback);
        } else {
            return Result.error("反馈创建失败");
        }
    }

    @GetMapping("/list")
    public Result<List<Feedback>> listFeedbacks() {
        List<Feedback> feedbacks = feedbackService.list();
        return Result.success(feedbacks);
    }

    @PutMapping("/update")
    public Result<Feedback> updateFeedback(@RequestBody Feedback feedback) {
        boolean updated = feedbackService.updateById(feedback);
        if (updated) {
            return Result.success(feedback);
        } else {
            return Result.error("反馈更新失败");
        }
    }

    @DeleteMapping("/delete/{feedbackId}")
    public Result<Boolean> deleteFeedback(@PathVariable int feedbackId) {
        boolean deleted = feedbackService.removeById(feedbackId);
        if (deleted) {
            return Result.success(true);
        } else {
            return Result.error("反馈删除失败");
        }
    }
}